refresh = {};
function returnDocument(){
    var file_name = document.location.href;
    var end = (file_name.indexOf("?") == -1) ? file_name.length : file_name.indexOf("?");
    return file_name.substring(file_name.lastIndexOf("/") + 1, end);
}

/*
 * setupRefresh
 *
 * base - root filesystem location of your site's document and styles.
 *
 * serverUrl - where this document is hosted.
 *
 * softHTMLRefresh - Only load body changes, browser does not refresh.
 *                   Disable if using JS or event listeners will disconnect.
 *
 */
function setupRefresh(base /* String */, serverUrl /* String */, softHTMLRefresh /* Boolean */){
    var undefined;
    //Force softHTMLRefresh to default as true
    var softHTMLRefresh = (softHTMLRefresh == undefined || softHTMLRefresh);
    if (!base) {
        alert("Oops you need to specify a location for the files!");
    }
    else {
        /*
         * Create a register of the document and style locations
         * with non-meaningful initial values.
         */
        refresh.location = base;
        refresh.stylesheets = {};
        $("head link[rel='stylesheet']").each(function(index, sheet){
            refresh.stylesheets[$(sheet).attr("href")] = 1;
        });
        refresh.document = {
            "file": returnDocument(),
            "time": 1
        };
    }
    /*
     * Polls the server periodically to check if anything has changed
     */
    setInterval(function(){
        $.getJSON(serverUrl, {
            "query": JSON.stringify(refresh),
            "base": refresh.base
        }, function(rst){
            for (stylesheet in rst.stylesheets) {
                if (rst.stylesheets[stylesheet] != refresh.stylesheets[stylesheet]) {
                    refresh.stylesheets[stylesheet] = rst.stylesheets[stylesheet];
                    /*
                     * If a stylesheet has changed delete the old one and make a new one.
                     * Unique url variable sent to prevent caching.
                     */
                    $("head link[rel='stylesheet'][href^='" + stylesheet + "']:eq(0)").remove();
                    $("head").prepend($(document.createElement("link")).attr({
                        "rel": "stylesheet",
                        "href": (stylesheet + "?" + refresh.stylesheets[stylesheet])
                    }));
                }
            }
            if (softHTMLRefresh && refresh.document.time != rst.document.time) {
                refresh.document.time = rst.document.time;
                $.get(refresh.document.file + "?" + refresh.document.time, {}, function(html){
                    /*
                     * The document has changed so let's overwrite the old body with the new body.
                     */
                    var fixml = html.replace(/\n/g, " ");
                    fixml = fixml.replace(/\s/g, ' ').replace(/  ,/g, ',');
                    var body = fixml.match(/<body\b[^>]*>(.*?)<\/body>/);
                    $("body").attr("innerHTML", body[1]);
                });
            }
            else 
                if (!softHTMLRefresh) {
                    /*
	                 * Little fix to stop it from refreshing when changes have not been made
	                 */
                    if (refresh.document.time == 1) 
                        refresh.document.time = rst.document.time;
                    if ((rst.document.time - refresh.document.time) > 0) {
                        document.location.href = refresh.document.file + "?" + refresh.document.time;
                        refresh.document.time = rst.document.time;
                    }
                }
        });
    }, 1000);
}
